//
//  RHMQAddEndPointController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/8/21.
//

#import "RHMQAddEndPointController.h"
#import "RHMQEndPointModel.h"
#import "RHMQSelPointTypeController.h"
#import "RHMQTTManage.h"
#import "RHMQDevModel.h"
#import "RHChioceRegionTypeVC.h"


@interface RHMQAddEndPointController ()<UITextFieldDelegate>

@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) QMUILabel *codeLab;

@property (nonatomic ,strong) UITextField *deviceTF;

@property (nonatomic ,strong) UITextField *secretTF;

@property (nonatomic ,strong) RHMQEndPointModel *model;

@property (nonatomic ,strong) RHMQEndPointModel *curModel;


@end

@implementation RHMQAddEndPointController

- (instancetype)initWithModel:(RHMQEndPointModel *)endPointModel{
    self = [super init];
    if(self){
        _model = endPointModel;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"新增端点"];
    [self.view setBackgroundColor:CFTabViewBgColor];
    [self setupUI];
}


- (void)setupUI{
    
    UIScrollView *scroll = [[UIScrollView alloc] init];
    [self.view addSubview:scroll];
    scroll.backgroundColor = [UIColor clearColor];
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor clearColor];
    [scroll addSubview:contentView];
    
    QMUILabel *devTapLab = [[QMUILabel alloc] init];
    [devTapLab setTextAlignment:NSTextAlignmentLeft];
    [devTapLab setFont:[UIFont systemFontOfSize:13]];
    [devTapLab setTextColor:kCOLOR_HEX(@"666666")];
    [devTapLab setText:@"设备ID"];
    [contentView addSubview:devTapLab];
    
    UIView *devView = [[UIView alloc] init];
    [devView setBackgroundColor:UIColor.whiteColor];
    [contentView addSubview:devView];
    
    UITextField *devTextField = [[UITextField alloc] init];
    [devTextField setPlaceholder:@"请输入设备ID"];
    [devTextField setFont:[UIFont systemFontOfSize:15]];
    [devTextField setTextColor:kCOLOR_HEX(@"666666")];
    [devView addSubview:devTextField];
    [devTextField setEnabled:NO];
    _deviceTF = devTextField;
    
    QMUILabel *secretTapLab = [[QMUILabel alloc] init];
    [secretTapLab setTextAlignment:NSTextAlignmentLeft];
    [secretTapLab setFont:[UIFont systemFontOfSize:13]];
    [secretTapLab setTextColor:kCOLOR_HEX(@"666666")];
    [secretTapLab setText:@"端点SN"];
    [contentView addSubview:secretTapLab];
    
    UIView *secretView = [[UIView alloc] init];
    [secretView setBackgroundColor:UIColor.whiteColor];
    [contentView addSubview:secretView];
    
    UITextField *secretTextField = [[UITextField alloc] init];
    [secretTextField setPlaceholder:@"请输入端点SN"];
    [secretTextField setFont:[UIFont systemFontOfSize:15]];
    [secretTextField setTextColor:kCOLOR_HEX(@"666666")];
    [secretView addSubview:secretTextField];
    [secretTextField setKeyboardType:UIKeyboardTypeDefault];
    _secretTF = secretTextField;
    
    QMUILabel *tapLab = [[QMUILabel alloc] init];
    [tapLab setTextAlignment:NSTextAlignmentLeft];
    [tapLab setFont:[UIFont systemFontOfSize:13]];
    [tapLab setTextColor:kCOLOR_HEX(@"666666")];
    [tapLab setText:@"端点类型"];
    [contentView addSubview:tapLab];
    
    UIView *view = [[UIView alloc] init];
    [view setBackgroundColor:UIColor.whiteColor];
    [contentView addSubview:view];
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentLeft];
    [_titleLab setFont:[UIFont systemFontOfSize:15]];
    [_titleLab setTextColor:kCOLOR_HEX(@"999999")];
    [_titleLab setText:@"请选择端点类型"];
    [view addSubview:_titleLab];
    
    _codeLab = [[QMUILabel alloc] init];
    [_codeLab setTextAlignment:NSTextAlignmentRight];
    [_codeLab setFont:[UIFont systemFontOfSize:15]];
    [_codeLab setTextColor:kCOLOR_HEX(@"666666")];
    [_codeLab setText:@""];
    [view addSubview:_codeLab];
    
    UIImageView *arrowV = [[UIImageView alloc] init];
    [arrowV setImage:[UIImage imageNamed:@"arrow"]];
    [view addSubview:arrowV];
    
    
    QMUILabel *tapLab3 = [[QMUILabel alloc] init];
    [tapLab3 setTextAlignment:NSTextAlignmentLeft];
    [tapLab3 setFont:[UIFont systemFontOfSize:13]];
    [tapLab3 setTextColor:kCOLOR_HEX(@"666666")];
    [tapLab3 setText:@"端点"];
    [contentView addSubview:tapLab3];
    
    UIView *view3 = [[UIView alloc] init];
    [view3 setBackgroundColor:UIColor.whiteColor];
    [contentView addSubview:view3];
    
    UITextField *textField3 = [[UITextField alloc] init];
    [textField3 setPlaceholder:@"请输入端点"];
    [textField3 setFont:[UIFont systemFontOfSize:15]];
    [textField3 setTextColor:kCOLOR_HEX(@"666666")];
    [view3 addSubview:textField3];
    textField3.delegate = self;
    
    
    QMUILabel *tapLab4 = [[QMUILabel alloc] init];
    [tapLab4 setTextAlignment:NSTextAlignmentLeft];
    [tapLab4 setFont:[UIFont systemFontOfSize:13]];
    [tapLab4 setTextColor:kCOLOR_HEX(@"666666")];
    [tapLab4 setText:@"软件版本"];
    [contentView addSubview:tapLab4];
    
    UIView *view4 = [[UIView alloc] init];
    [view4 setBackgroundColor:UIColor.whiteColor];
    [contentView addSubview:view4];
    
    UITextField *textField4 = [[UITextField alloc] init];
    [textField4 setPlaceholder:@"请输入软件版本"];
    [textField4 setFont:[UIFont systemFontOfSize:15]];
    [textField4 setTextColor:kCOLOR_HEX(@"666666")];
    [textField4 setKeyboardType:UIKeyboardTypeDecimalPad];
    [view4 addSubview:textField4];
    kWeakSelf;
    [[RACSignal merge:@[textField4.rac_textSignal,RACObserve(textField4, text)]]subscribeNext:^(NSString  *_Nullable text) {
        weakSelf.curModel.lastVersion = text;
    }];
    
    
    QMUILabel *tapLab5 = [[QMUILabel alloc] init];
    [tapLab5 setTextAlignment:NSTextAlignmentLeft];
    [tapLab5 setFont:[UIFont systemFontOfSize:13]];
    [tapLab5 setTextColor:kCOLOR_HEX(@"666666")];
    [tapLab5 setText:@"硬件版本"];
    [contentView addSubview:tapLab5];
    
    UIView *view5 = [[UIView alloc] init];
    [view5 setBackgroundColor:UIColor.whiteColor];
    [contentView addSubview:view5];
    
    UITextField *textField5 = [[UITextField alloc] init];
    [textField5 setPlaceholder:@"请输入硬件版本"];
    [textField5 setFont:[UIFont systemFontOfSize:15]];
    [textField5 setTextColor:kCOLOR_HEX(@"666666")];
    [textField5 setKeyboardType:UIKeyboardTypeDecimalPad];
    [view5 addSubview:textField5];
    [[RACSignal merge:@[textField5.rac_textSignal,RACObserve(textField5, text)]]subscribeNext:^(NSString  *_Nullable text) {
        weakSelf.curModel.hardVersion = text;
    }];
    
    UITapGestureRecognizer *tapView = [[UITapGestureRecognizer alloc] init];
    view.userInteractionEnabled = YES;
    [view addGestureRecognizer:tapView];
    

    QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:CFMainColor];
    [btn.layer setCornerRadius:8];
    [btn setTitle:@"新增" forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
    [contentView addSubview:btn];

  
    [scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(weakSelf.view);
    }];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(scroll);
        make.width.mas_equalTo(scroll);
        make.height.greaterThanOrEqualTo(@0.f);//此处保证容器View高度的动态变化 大于等于0.f的高度
    }];

    [devTapLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(20);
    }];
    
    [devView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(devTapLab.mas_bottom).offset(5);
        make.height.mas_equalTo(50);
    }];
    
    [devTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    
    [tapLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(devView.mas_bottom).offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(20);
    }];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(tapLab.mas_bottom).offset(5);
        make.height.mas_equalTo(50);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_offset(0);
        make.left.mas_offset(15);
        make.width.mas_equalTo(200);
    }];
    
    [_codeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab.mas_right);
        make.top.bottom.equalTo(weakSelf.titleLab);
        make.right.equalTo(arrowV.mas_left).offset(-5);
    }];
    
    [arrowV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.right.mas_offset(-15);
        make.centerY.mas_offset(0);
    }];
    
    
    [secretTapLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(view.mas_bottom).offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(20);
    }];
    
    [secretView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(secretTapLab.mas_bottom).offset(5);
        make.height.mas_equalTo(50);
    }];
    
    [secretTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    
    [tapLab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(secretView.mas_bottom).offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(20);
    }];
    
    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(tapLab3.mas_bottom).offset(5);
        make.height.mas_equalTo(50);
    }];
    
    [textField3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    
    [tapLab4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(view3.mas_bottom).offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(20);
    }];
    
    [view4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(tapLab4.mas_bottom).offset(5);
        make.height.mas_equalTo(50);
    }];
    
    [textField4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    [tapLab5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(view4.mas_bottom).offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(20);
    }];
    
    [view5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(tapLab5.mas_bottom).offset(5);
        make.height.mas_equalTo(50);
    }];
    
    [textField5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(view5.mas_bottom).offset(50);
        make.bottom.mas_offset(-kBottomToolBar-50);
        make.height.mas_equalTo(50);
    }];
    
    
    if(!_model){
        [tapView.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
          [weakSelf selectPointType];
        }];
    }else{
        _curModel = [[RHMQEndPointModel alloc] init];
        if([_model.type isEqualToString:@"EMS"]){
            [self.titleLab setText:@"Master"];
            [_curModel setType:self.titleLab.text];
            [_curModel setTypeCode:self.titleLab.text];
            [_curModel setModel:self.titleLab.text];
        }
        if([_model.type isEqualToString:@"Master"]){
            [self.titleLab setText:@"Pack"];
            [_curModel setType:self.titleLab.text];
            [_curModel setTypeCode:self.titleLab.text];
            [_curModel setModel:self.titleLab.text];
        }
        [weakSelf.titleLab setTextColor:kCOLOR_HEX(@"666666")];
    }
    
    
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf addEndPoint];
    }];
    
    [self.deviceTF setText:[RHMQTTManage shareMQTTManage].devModel.devId];
    [self.secretTF setText:@""];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [QMUITips showLoadingInView:self.view];
    [BaseModel startGetWithCompletionAPIKey:D_endpointExampleList argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        NSArray *arr = request.responseJSONObject[@"rows"];
        NSMutableArray *typeArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            [typeArr addObject:ValidNullData(dic[@"endpointExampleCode"])];
        }
        RHChioceRegionTypeVC *vc = [[RHChioceRegionTypeVC alloc]initWithData:typeArr.copy];
        kWeakSelf;
        vc.selectBlock = ^(NSInteger index) {
            weakSelf.curModel.endpoint = typeArr[index];
            textField.text = typeArr[index];
        };
        [[RHWindowTipManage shareWindowTipManage] showWithController:vc];
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
    
    }];
    //固件类型
   
    
    return NO;
}

#pragma mark - Action

- (void)selectPointType{
    RHMQSelPointTypeController *vc = [[RHMQSelPointTypeController alloc] initWithType];
    [self.navigationController pushViewController:vc animated:YES];
    kWeakSelf;
    vc.callBackBlock = ^(RHMQEndPointModel * _Nonnull model ,NSString *deviceTypeCode) {
        [weakSelf.titleLab setText:model.type];
        [weakSelf.titleLab setTextColor:kCOLOR_HEX(@"666666")];
        weakSelf.curModel = model;
    };
}

- (void)addEndPoint{
    if(!self.curModel) return;
    self.curModel.sn = self.secretTF.text;
    if(self.callBackBlock){
        self.callBackBlock(self.curModel);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
